//
//  MainTableViewCell.m
//  HDYS
//
//  Created by qianfeng on 15-4-21.
//  Copyright (c) 2015年 kay. All rights reserved.
//

#import "MainTableViewCell.h"
#import "UIButton+WebCache.h"
#import "DBManager.h"

#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width


@interface MainCellBtn : UIButton

@end
@implementation MainCellBtn

-(void)setHighlighted:(BOOL)highlighted {}

@end

@interface MainTableViewCell ()

@property (nonatomic, strong) MainCellBtn *bigImgBtn;
@property (nonatomic, strong) UILabel *des_longLabel;
@property (nonatomic, strong) UILabel *priceLabel;


@property (nonatomic, strong) MainDataModel *curGoodsModel;

@end

@implementation MainTableViewCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self createSubViews];
    }
    return self;
}

-(void)createSubViews{
    MainCellBtn *bigImageBtn = [MainCellBtn buttonWithType:UIButtonTypeCustom];
    bigImageBtn.frame = CGRectMake(0, 0, SCREEN_WIDTH, 360);
    UIImage *image = [UIImage imageNamed:@"bs_guide_default1"];
    UIImage *newImage = [image stretchableImageWithLeftCapWidth:10 topCapHeight:10];
    [bigImageBtn setBackgroundImage:newImage forState:UIControlStateNormal];
    [bigImageBtn addTarget:self action:@selector(bigImageBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:bigImageBtn];
    self.bigImgBtn = bigImageBtn;
    
    UILabel *des_longLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, bigImageBtn.frame.origin.y + bigImageBtn.frame.size.height + 5, SCREEN_WIDTH - 20, 40)];
    des_longLabel.font = [UIFont systemFontOfSize:14];
    des_longLabel.numberOfLines = 0;
    des_longLabel.lineBreakMode = NSLineBreakByCharWrapping;
    des_longLabel.text = @"商品介绍";
    [self.contentView addSubview:des_longLabel];
    self.des_longLabel = des_longLabel;
    
    UILabel *priceLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, des_longLabel.frame.origin.y + des_longLabel.frame.size.height + 5, 80, 30)];
    priceLabel.text = @"998";
    priceLabel.font = [UIFont systemFontOfSize:18];
    [self.contentView addSubview:priceLabel];
    self.priceLabel = priceLabel;
    
    
    UIButton *likeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    likeBtn.frame = CGRectMake(priceLabel.frame.origin.x + priceLabel.frame.size.width + 40 + 100, priceLabel.frame.origin.y , 80, 25);
    [likeBtn setTitle:@"加入收藏" forState:UIControlStateNormal];
    likeBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    [likeBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [likeBtn addTarget:self action:@selector(likeBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:likeBtn];
    self.likeBtn = likeBtn;
    
}

#pragma mark - 按钮点击
/**
 *  点击大图,进入宝贝详情
 */
-(void)bigImageBtnClick:(UIButton *)btn{
    if ([self.delegate respondsToSelector:@selector(mainTableViewCellWillGoToGoodsInfoWithModel:)]) {
        [self.delegate performSelector:@selector(mainTableViewCellWillGoToGoodsInfoWithModel:) withObject:self.curGoodsModel];
    }
}

//加入收藏
-(void)likeBtnClick:(UIButton *)btn{
    if ([btn.titleLabel.text isEqualToString:@"加入收藏"]) {
        [btn setTitle:@"取消收藏" forState:UIControlStateNormal];
        NSLog(@"self.curGoodsModel = %@",self.curGoodsModel);
        if ([self.delegate respondsToSelector:@selector(mainTableViewCellWillCollectGoodsWithModel:andIsCollect:)]) {
            [self.delegate mainTableViewCellWillCollectGoodsWithModel:self.curGoodsModel andIsCollect:YES];
        }
    } else {
        [btn setTitle:@"加入收藏" forState:UIControlStateNormal];
        if ([self.delegate respondsToSelector:@selector(mainTableViewCellWillCollectGoodsWithModel:andIsCollect:)]) {
            [self.delegate mainTableViewCellWillCollectGoodsWithModel:self.curGoodsModel andIsCollect:NO];
        }
    }
}

- (void)configureUIWithModel:(MainDataModel *)model{
    self.curGoodsModel = model;
    NSString *newStr = [@"http://mls.coderss.cn/Public/Uploads/goods/" stringByAppendingString:model.pic];
    [self.bigImgBtn setBackgroundImageWithURL:[NSURL URLWithString:newStr] forState:UIControlStateNormal];
    self.des_longLabel.text = model.gname;
    self.priceLabel.text = model.price;
}




@end
